package com.easepal7506a.project.ui.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseScanAndAdjust;
import com.easepal7506a.project.ui.iview.IShoulderAdjustView7506;
import com.easepal7506a.project.ui.presenter.ShoulderAdjustPresenter7506;
import com.example.reslib.utils.ToastUtils;
import com.ogawa.base.utils.CommonUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 肩部调整
 */
public class ShouldersAdjustmentActivity7506 extends BaseActivity implements View.OnClickListener,IShoulderAdjustView7506 ,CloseScanAndAdjust.onCloseActivity {


    private ImageView mIvUp;
    private ImageView mIvDown;
    private int count = 1;
    private int mPoint;//机芯位置
    private ShoulderAdjustPresenter7506 mPresenter;
    private boolean isClick =false;
    private long mClickTime  =0;
    private String mAction="";//按上up   按下dowm
    private Disposable mDisposable;
    @Override
    public int setLayoutId() {
        return R.layout.activity_shoulders_adjustment_7506;
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
    }


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestoty();
        CloseScanAndAdjust.getInst().removeObserver(this);
    }



    private void initView() {
        mPresenter = new ShoulderAdjustPresenter7506(this);
        mPoint =Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusPointPosition)) ;
        Log.e("getPoint_init",mPoint+"");
        mIvUp =findViewById(R.id.img_shoulder_up);
        mIvDown =findViewById(R.id.img_shoulder_down);
        findViewById(R.id.img_shoulder_ok).setOnClickListener(this);
        mIvUp.setOnClickListener(this);
        mIvDown.setOnClickListener(this);
        CloseScanAndAdjust.getInst().registerObserver(this);
    }

    private void setClick(){
        isClick =true;
        mClickTime = System.currentTimeMillis();
        mIvUp.setEnabled(false);
        mIvDown.setEnabled(false);
        clickButton();
    }
    public void showUpDown(){
        isClick = false;
        mIvUp.setEnabled(true);
        mIvDown.setEnabled(true);
        if(mDisposable!=null){
            mDisposable.dispose();
        }

    }
    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.img_shoulder_ok) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.JIANBQR);
                this.finish();
            }

        }
        else   if (i == R.id.img_shoulder_up) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                Log.e("getPoint_up",mPoint+"");
                if(mPoint<10){
                    mAction="up";
                    setClick();
                    sendCommond(CommmandNum.JIXINUP);
                }else {
                    ToastUtils.showShortToast(this,"机芯已在最高处");
                }

            }

        }
        else   if (i == R.id.img_shoulder_down) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                Log.e("getPoint_down",mPoint+"");
                if(mPoint>0){
                    mAction="down";
                    setClick();
                    sendCommond(CommmandNum.JIXINDOWM);
                }else {
                    ToastUtils.showShortToast(this,"机芯已在最低处");
                }

            }

        }
    }


    public void clickButton(){
        Observable.interval(0,300, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable =d;
                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        if(isClick&&System.currentTimeMillis()-mClickTime>3000){
                            showUpDown();

                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showUpDown();

                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    @Override
    public void isFinish() {
        this.finish();

    }

    @Override
    public void getPoint(int position) {
        //如果是点击了上
        if(!TextUtils.isEmpty(mAction)&&mAction.equals("up")){
            if(position>mPoint){
                showUpDown();
                mAction ="";
            }
        }
       else if(!TextUtils.isEmpty(mAction)&&mAction.equals("down")){
            if(position<mPoint){
                showUpDown();
                mAction ="";
            }
        }
        mPoint = position;


    }

    @Override
    public void onClose() {
        this.finish();
    }






}
